<template>
    <div class="app-container">
        <el-card class="box-card">
            <div class="flex justify-between">
                <el-button type="primary" size="default" @click="handleAdd">添加</el-button>
            </div>
            <el-table
                ref="multipleTable"
                v-loading="loading"
                :data="tableData"
                border
                class="mt-3"
                stripe
                style="width: 100%"
                tooltip-effect="dark"
            >
                <el-table-column label="序号" type="index" :index="indexMethod" prop="user_code" width="100px"></el-table-column>
                <el-table-column label="主题内容" prop="title"></el-table-column>
                <el-table-column label="排序" prop="sort"></el-table-column>
                <el-table-column label="创建时间" prop="created_at"></el-table-column>
                
                <el-table-column label="操作" width="200px">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            @click="handleEdit(scope.row)"
                        >编辑
                        </el-button>
                        <!-- <el-button
                            size="mini"
                            type="text"
                            @click="handleGive(scope.row)"
                        >赠送
                        </el-button>
                        <el-button
                            size="mini"
                            type="text"
                            @click="handleSDelete(scope.row)"
                        >删除
                        </el-button> -->
                    </template>
                </el-table-column>
            </el-table>
    
            <!-- 分页 -->
            <cus-pagination
                v-show="total > 0"
                :limit.sync="listQuery.perPage"
                :page.sync="listQuery.page"
                :total="total"
                @pagination="getTableData"
            />
        </el-card>
  
        <el-dialog
            :visible.sync="dialogVisible"
            :title="dialogTitle"
            width="30%"
            @close="closeDialog('form')"
        >
            <el-form ref="form" :inline="false" :model="formData" :rules="rules1" label-width="120px" size="normal">
                <el-form-item label="房间主题" prop="title">
                    <el-input
                        v-model="formData.title"
                        placeholder="请输入房间主题"
                    />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input
                        v-model="formData.sort"
                        placeholder="请输入排序"
                        resize="none"
                    />
                </el-form-item>
            </el-form>
    
            <span slot="footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleOnSubmit">确定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
  
<script>
    import { listData, themeSave } from '@/api/privateRoomManagement'
  
  	export default {
        name: 'roomTheme',
        components: {},
        data() {
            return {
                dialogVisible: false,
                dialogTitle: undefined,
                formData: {
                    title: undefined,
                    sort: undefined
                },
                rules1: {
                    title: [{ required: true, message: '请输入房间主题', trigger: 'blur' }]
                },
                loading: false,
                tableData: [],
                listQuery: {
                    page: 1,
                    perPage: 10
                },
                total: 1,
                options: [
                    {
                        value: 1,
                        label: '123'
                    }
                ],
                fileList1: undefined,
                fileList2: undefined,
                fileList3: undefined
            }
        },
        created() {
            this.getTableData()
        },
        methods: {
            indexMethod(index) {
                return this.total - (this.listQuery.page-1) * this.listQuery.perPage - index
            },
            // 网络请求
            /**
             * getTableData
             * @desc 获取表格数据
             */
            async getTableData() {
                this.loading = true
                for (let key in this.listQuery) {
                    this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key]
                }
                const detail = await listData(this.listQuery)
                if (detail.code === 200) {
                    this.tableData = detail.data.list
                    this.total = detail.data.meta.total
                }
                this.loading = false
        
            },
            closeDialog(form) {
                if (this.$refs[form]) {
                    this.$refs[form].clearValidate() // 清除整个表单的校验
                    this.$refs[form].resetFields() // 清除整个表单的校验
                }
                this.formData = {
                    title: undefined,
                    sort: undefined
                }
            },
            
            handleOnSubmit(scope) {
                
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        
                        for (let key in this.formData) {
                            this.formData[key] = this.formData[key] === '' || this.formData[key] === undefined ? 0 : this.formData[key]
                        }
                        themeSave(this.formData).then(res => {
                            if(res.code === 200) {
                                this.$message({
                                    type: 'success',
                                    message: '操作成功'
                                })
                                this.dialogVisible = false
                                this.getTableData()
                            }
                        })
                        
                    }
                })
            },
            handleEdit(row) {
                this.dialogTitle = '编辑'
                this.dialogVisible = true
                this.formData = {
                    title: row.title,
                    sort: row.sort,
                    id: row.id
                }
            },
            handleGive(row) {
                if(type == 1) {
                    this.$confirm('确定上架该奖励？上架后将显示在客户端', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        
                        
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
                if(type == 2) {
                    this.$confirm('确定下架该奖励？下架后将不显示在客户端', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.awardList.splice(index,1)
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消下架'
                        });
                    });
                }
                
            },
            handleSDelete(row) {
                this.$confirm('确定删除该奖励？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.awardList.splice(index,1)
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleReset() {
                this.listQuery = {
                    page: 1,
                    perPage: 10
                }
                this.getTableData()
            },
            handleAdd() {
                this.dialogTitle = '添加'
                this.dialogVisible = true
            },
        }
    }
</script>
  
<style lang="scss" scoped>
.infoBox {
  margin-top: 15px;
  margin-bottom: 15px;
  padding-bottom: 15px;
  border: 1px solid #999999;
  border-radius: 10px;
  display: flex;
  align-items: baseline;
  justify-content: center;
  box-sizing: border-box;
  padding-right: 30px;
  .info_1{
    display: flex;
    justify-content: center;
    flex-direction: column;
    width: 100%;

    .info_2{
      margin-top: 15px;
      display: flex;
      align-items: center;
	  position: relative;

      span{
        width: 180px;
        text-align: right;
      }
	  .tip{
			color: red;
			font-size: 12px;
			position: absolute;
			top: 100%;
			left: 120px;
		}
    }
  }
}
.item {
    width: 100px;
}
.formItemBox{
	display: flex;
	align-items: center;
	flex-wrap: wrap;

	width: 100%;
	overflow: hidden;
}
::v-deep {
	.formItemBox{
		.el-form-item {
			width: 50% !important;
		}
	}
    
}

</style>
  